(function() {
  //点击弹框的 确定 按钮，弹框消失
  $('.weui_btn_dialog.primary').on('click',function() {
      $(this).parents('.weui_dialog_alert').hide();
  });

  var loginValidate = {

    init: function() {
      this.bindEvent();
    },
    bindEvent: function(){
      this.clickLoginBtn();
    },

    clickLoginBtn: function() {
      var _this = this;

      var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
      $('.login-btn').on('click',function(){

         if( !$('.register-phone-num').val() ) {
            $('.weui_dialog_bd.prompt_text').html('请输入手机号');
            $('.weui_dialog_alert').show();
            return false;
        }

        if(!myreg.test($(".register-phone-num").val())){
            $('.weui_dialog_bd.prompt_text').html('请输入正确手机号');
            $('.weui_dialog_alert').show();
            return false;
        }

        if( $('.phoneCodeNum').val() == '' ) {
            $('.weui_dialog_bd.prompt_text').html('请输入密码');
            $('.weui_dialog_alert').show();
            return false;

        }

           _this.loginProcess();
          console.log( $('.login-phone-num').val(),$('.login-psw').val() );
          debugger;
      })
    },
    loginProcess: function() {

      var _this = this;
       _this.rest({
          methodId: "4002",
          data: {
              "phone": $('.register-phone-num').val(),
              "password":$('.phoneCodeNum').val()
          },
          success: function (res) {

              if (res) {
                console.log( "login success!",res );
                if ("0" === res.code) {
                  var data = res.res.data;
                  _this.getLandlordStatus( data.landlordId );
                  //window.location.href="entrence.html?landlordId="+data.landlordId;
                } else {
                    var tip = res.res.msg;
                      $('.base-info').find('.weui_dialog_bd.prompt_text').html(tip);
                     $('.base-info').find('.weui_dialog_alert').show();
                }
              }
          },
          error: function (res) {
            console.log( res );
          }
        })
    },
    getLandlordStatus: function(landlordId) {
       this.rest({
          methodId: "4012",
          data: {
              "landlordId": landlordId
          },
          success: function (res) {

              if (res) {
                console.log( "landLordStatus",res );
                if ("0" === res.code) {
                    var data = res.res.data;
                    console.log( data );
                    // status  -1=未申请 0=申请中 1=审核通过 2=审核不通过
                    // identityType [0=未申请 1=业主 2=二房东（个人） 3=二房东（公司）]
                    if( data.status == '0' ){
                      window.location.href = 'checking.html';
                    } else if( data.status == '1' ) {
                      window.location.href = 'checkSuccess.html';
                    } else if( data.status == '2' ) {
                      window.location.href = 'checkLose.html?landlordId=' + landlordId + '&identityType='+data.identityType+'&checkId='+data.checkId;
                    } else {
                      window.location.href="entrence.html?landlordId="+landlordId;
                    }
                } else {
                    var tip = res.res.msg;
                    $('.all-info').find('.weui_dialog_bd.prompt_text').html(tip);
                    $('.all-info').find('.weui_dialog_alert').show();
                }
              }
          },
          error: function (res) {
            console.log( res );
          }
        })
    },
    rest: function(opt){
      var _this = this;
      if(!opt.methodId){
        throw new Error("methodId 不能为空！")
      }
      if(!opt.data){
        opt.data = {}
      }
      $.ajax({
        url: global.uip.url,
        data: postdata(opt.methodId, opt.data),
        type: "post",
        success: opt.success,
        error: opt.error,
        complete: opt.complete
      })
    },
    orderObj: function(obj) {
      let o = {};
      Object.keys(obj).forEach((key) => {
        o[key] = obj[key];
      });
      return o;
    },
    param: function(obj) {
      let arr = [];
      for (let i in obj) {
        arr.push(i + '=' + obj[i]);
      }
      return arr.join('&');
    }
  };
  loginValidate.init();

})();
